import { NgExpressEngineResponse } from '../framework/index'
import * as express from 'express'
import * as core from 'express-serve-static-core'
import { join } from 'path'
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'
import { ngExpressEngine } from '@nguniversal/express-engine'

const DIST_FOLDER = join(__dirname, 'install/browser')
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./install/server/main.js')

export const SERVER_ROOT = __dirname
export const htmlEngine: NgExpressEngineResponse = ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [provideModuleMap(LAZY_MODULE_MAP)]
})

export function createHtmlEngine(): core.Express {
  const expressApp = express()
  expressApp.engine('html', htmlEngine)
  expressApp.set('view engine', 'html')
  expressApp.set('views', join(DIST_FOLDER))
  expressApp.get(
    '*.*',
    express.static(join(DIST_FOLDER), {
      maxAge: 0
    })
  )
  return expressApp
}
